package code.temp.thread;

import com.github.cosycode.common.ext.hub.Throws;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * <b>Description : </b>
 *
 * @author CPF
 * @date 2021/5/8 15:57
 **/
public class FutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        System.currentTimeMillis();
//
//        long start = System.currentTimeMillis();
//
//        // 等凉菜
//        Callable<String> ca1 = () -> {
//            Throws.con(1000, Thread::sleep);
//            return "凉菜准备完毕";
//        };
//        Callable<String> ca2 = () -> {
//            Throws.con(1000 * 3, Thread::sleep);
//            return "包子准备完毕";
//        };
//        FutureTask<String> ft1 = new FutureTask<>(ca1);
//        FutureTask<String> ft2 = new FutureTask<>(ca2);
//
//        new Thread(ft1).start();
//        new Thread(ft2).start();
//
//        System.out.println(ft1.get());
//        System.out.println(ft2.get());
//
//        long end = System.currentTimeMillis();
//        System.out.println("准备完毕时间："+(end-start));
//



        Callable privateAccount= () -> {
            Thread.sleep(5*1000);
            int total=new Integer(new Random().nextInt(10000));
            System.out.println("您个人账户上还有"+total+" 存款可以支配");
            return total;
        };
        FutureTask task=new FutureTask(privateAccount);
        //创建新线程获取个人账户信息
        Thread thread=new Thread(task);
        thread.start();

        int total=new Random().nextInt(1000);
        System.out.println("主线程在这工作");
        System.out.println("您目前操作金额为： "+total+" .");
        System.out.println("请等待计算个人账户的金额");
        while(!task.isDone()){//判断是否已经获取返回值
            try {
                Thread.sleep(3*1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        Integer privateSingle=(Integer)task.get();
        int post=privateSingle.intValue();

        System.out.println("您当前账户共有金额为:"+(total+post)+" ￥");
    }

}
